;;;; init.el --- Where all the magic begins
;;;;
;;;; Evgeniy Sharapov, evgeniy.sharapov@gmail.com
;;;;

;; Set the debug option to enable a backtrace when a
;; problem occurs.
(setq debug-on-error t)

;;
;; Some program locations 
;;
(defvar loc-sbcl "c:/workspace/sbcl/1.0.13"
  "Path to SBCL lisp")
;; 
;; Setup load paths
;; 
(defun set-load-paths (this-directory &optional with-subdirs recursive)
  "Add THIS-DIRECTORY at the beginning of the load-path, if it exists. Add all its subdirectories not starting with a '.' if the optional argument WITH-SUBDIRS is not nil. Do it recursively if the third argument is not nil."
  ;; TODO: exclude from the loading directories .emacs.d/auto-save-list and .emacs.d/server
  (when this-directory
    (when (file-directory-p this-directory)
      (let* ((this-directory (expand-file-name this-directory))
             (files (directory-files this-directory t "^[^\\.]")))
        ;; completely canonicalize the directory name (*may not* begin with `~')
        (while (not (string= this-directory (expand-file-name this-directory)))
          (setq this-directory (expand-file-name this-directory)))
        (message "Adding `%s' to load-path..." this-directory)
        (add-to-list 'load-path this-directory)
        (if with-subdirs
            (progn
              (while files
                (setq dir-or-file (car files))
                (when (file-directory-p dir-or-file)
                  (if recursive
                      (set-load-paths dir-or-file 'with-subdirs)
                    (set-load-paths dir-or-file)))
                (setq files (cdr files)))))))))
;;
;; Setup load paths
;;
(set-load-paths "~/.emacs.d" 'with-subdirs 'recursive)
;;
;; Setup custom file 
;;
(setq custom-file "~/.emacs.d/my-custom.el")
(load custom-file 'noerror)
;;
;; Speed up loading by compilation customiztion files
;; 
(defun autocompile ()
  "Compile itself if this is config file"
  (interactive)
  (if (or
       (string-match ".emacs.d/init-[a-z]+.el$" (buffer-file-name))
       (string-match ".emacs.d/init.el$" (buffer-file-name)))
      (byte-compile-file (buffer-file-name))))
(add-hook 'after-save-hook 'autocompile)

;; These should be loaded on startup rather than autoloaded on demand
;; since they are likely to be used in every session:
;(require 'cl)
;(require 'saveplace)
;(require 'ffap)
;(require 'uniquify)
;(require 'ansi-color)
;(require 'recentf)
;(require 'linum) ;; line numbers 
;(require 'highlight-symbol) ;; quickly highlight a symbol throughout the buffer and cycle through its locations. 
;(require 'highline)
;(require 'color-theme)

;;
;; Load init files 
;;
(defun load-init (modules)
  "Load initialization files"
  (mapc (lambda (name)
          (load (format "init-%s" name)))
        modules))

(load-init '(general
             colors
             funcs
             windows
             lisp
             eshell
             orgmode
             registers
             bindings
             ecb
             haskell
             python
             ruby
             modes
             xml
             yasnippet
             vcs
             ))

;;; init.el ends here
